using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration.Claims;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace ldapcp
{
    [Guid("9d26820f-15e8-4dbb-8bc6-4608c4920d8a")]
    public class LDAPCPFeatureReceiver : SPClaimProviderFeatureReceiver
    {
        public override string ClaimProviderAssembly
        {
            get { return typeof(LDAPCP).Assembly.FullName; }
        }

        public override string ClaimProviderDescription
        {
            get { return LDAPCP._ProviderInternalName; }
        }

        public override string ClaimProviderDisplayName
        {
            get { return LDAPCP._ProviderInternalName; }
        }

        public override string ClaimProviderType
        {
            get { return typeof(LDAPCP).FullName; }
        }

        private void ExecBaseFeatureActivated(Microsoft.SharePoint.SPFeatureReceiverProperties properties)
        {
            // Wrapper function for base FeatureActivated. 
            // Used because base keywork can lead to unverifiable code inside lambda expression
            base.FeatureActivated(properties);
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                LdapcpLoggingService svc = LdapcpLoggingService.Local;
            });
        }

        private void RemovePersistedObject()
        {
            var PersistedObject = LDAPCPConfig.GetFromConfigDB();
            if (PersistedObject != null)
                PersistedObject.Delete();
        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            ExecBaseFeatureActivated(properties);
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                this.RemovePersistedObject();
            });
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                base.RemoveClaimProvider(LDAPCP._ProviderInternalName);
                //var trust = LDAPCP.GetSPTrustAssociatedWithCP(LDAPCP._ProviderInternalName);
                //if (trust != null)
                //{
                //    trust.ClaimProviderName = null;
                //    trust.Update();
                //}
                this.RemovePersistedObject();
                LdapcpLoggingService.Unregister();
            });
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                this.RemovePersistedObject();
            });
        }

        public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, IDictionary<string, string> parameters)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                //this.RemovePersistedObject();
                LdapcpLoggingService svc = LdapcpLoggingService.Local;
            });
        }
    }
}
